home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp95 / freyja13.exe / lha / REGION.C < prev    next >
C/C++ Source or Header  |  1992-03-22  |  7KB  |  396 lines

  1. /* REGION.C -- Region-Oriented Commands
  2.  
  3.     Written July 1991 by Craig A. Finseth
  4.     Copyright 1991 by Craig A. Finseth
  5. */
  6.  
  7. #include "freyja.h"
  8.  
  9. void R_CopyToMark();    /* struct mark *mptr, FLAG isforward */
  10. void R_Dent();        /* void */
  11. void R_Do();        /* void (*proc)() */
  12. void R_HToS();        /* void */
  13. void R_Lower();        /* void */
  14. void R_SToH();        /* void */
  15. void R_Tabify();    /* void */
  16. void R_Trim();        /* void */
  17. void R_Untabify();    /* void */
  18. void R_Upper();        /* void */
  19.  
  20. /* ------------------------------------------------------------ */
  21.  
  22. /* Remove the trailing whitespace from a region. */
  23.  
  24. void
  25. RDelWhite()
  26.     {
  27.     R_Do(R_Trim);
  28.     }
  29.  
  30.  
  31. /* ------------------------------------------------------------ */
  32.  
  33. /* Convert hard newlines to soft. */
  34.  
  35. void
  36. RHardToSoft()
  37.     {
  38.     R_Do(R_HToS);
  39.     }
  40.  
  41.  
  42. /* ------------------------------------------------------------ */
  43.  
  44. /* Delete the specified region in the specified direction. */
  45.  
  46. void
  47. RKillToMark(mptr, isforward)
  48.     struct mark *mptr;
  49.     FLAG isforward;
  50.     {
  51.     R_CopyToMark(mptr, isforward);
  52.     BRegDelete(mptr);
  53.     }
  54.  
  55.  
  56. /* ------------------------------------------------------------ */
  57.  
  58. /* Indent the region. */
  59.  
  60. void
  61. RIndent()
  62.     {
  63.     if (!isuarg) uarg = cbuf->c.tab_spacing;
  64.     R_Do(R_Dent);
  65.     }
  66.  
  67.  
  68. /* ------------------------------------------------------------ */
  69.  
  70. /* Lowercase the region. */
  71.  
  72. void
  73. RLower()
  74.     {
  75.     R_Do(R_Lower);
  76.     }
  77.  
  78.  
  79. /* ------------------------------------------------------------ */
  80.  
  81. /* Set the mark to the point. */
  82.  
  83. void
  84. RMarkSet()
  85.     {
  86.     BMarkToPoint(mark);
  87.     }
  88.  
  89.  
  90. /* ------------------------------------------------------------ */
  91.  
  92. /* Swap the point and the mark. */
  93.  
  94. void
  95. RMarkSwap()
  96.     {
  97.     BMarkSwap(mark);
  98.     uarg = 0;
  99.     }
  100.  
  101.  
  102. /* ------------------------------------------------------------ */
  103.  
  104. /* Outdent the region. */
  105.  
  106. void
  107. ROutdent()
  108.     {
  109.     if (!isuarg) uarg = cbuf->c.tab_spacing;
  110.     uarg = -uarg;
  111.     R_Do(R_Dent);
  112.     }
  113.  
  114.  
  115. /* ------------------------------------------------------------ */
  116.  
  117. /* Copy the region to the kill buffer. */
  118.  
  119. void
  120. RRegCopy()
  121.     {
  122.     R_CopyToMark(mark, BIsBeforeMark(mark));
  123.     }
  124.  
  125.  
  126. /* ------------------------------------------------------------ */
  127.  
  128. /* Delete the region. */
  129.  
  130. void
  131. RRegDelete()
  132.     {
  133.     if (!isuarg) R_CopyToMark(mark, BIsBeforeMark(mark));
  134.     BRegDelete(mark);
  135.     }
  136.  
  137.  
  138. /* ------------------------------------------------------------ */
  139.  
  140. /* Convert soft newlines to hard. */
  141.  
  142. void
  143. RSoftToHard()
  144.     {
  145.     R_Do(R_SToH);
  146.     }
  147.  
  148.  
  149. /* ------------------------------------------------------------ */
  150.  
  151. /* Tabify the region. */
  152.  
  153. void
  154. RTabify()
  155.     {
  156.     R_Do(R_Tabify);
  157.     }
  158.  
  159.  
  160. /* ------------------------------------------------------------ */
  161.  
  162. /* Untabify the region. */
  163.  
  164. void
  165. RUntabify()
  166.     {
  167.     R_Do(R_Untabify);
  168.     }
  169.  
  170.  
  171. /* ------------------------------------------------------------ */
  172.  
  173. /* Uppercase the region. */
  174.  
  175. void
  176. RUpper()
  177.     {
  178.     R_Do(R_Upper);
  179.     }
  180.  
  181.  
  182. /* ------------------------------------------------------------ */
  183.  
  184. /* Yank the kill buffer. */
  185.  
  186. void
  187. RYank()
  188.     {
  189.     struct buffer *savebuf = cbuf;
  190.  
  191.     BMarkToPoint(mark);
  192.     BBufGoto(kill_buf);
  193.     BMoveToEnd();
  194.     BMarkToPoint(cwin->point);
  195.     BMoveToStart();
  196.     BRegCopy(cwin->point, savebuf);
  197.     BBufGoto(savebuf);
  198.     }
  199.  
  200.  
  201. /* ------------------------------------------------------------ */
  202.  
  203. /* Copy point to mark to delete buffer */
  204.  
  205. void
  206. R_CopyToMark(mptr, isforward)
  207.     struct mark *mptr;
  208.     FLAG isforward;
  209.     {
  210.     struct mark *mptr2;
  211.     struct buffer *savebuf = cbuf;
  212.  
  213.     BBufGoto(kill_buf);
  214.     if (!TabIsDelete(lastkey, lasttable) && !(isrepeating &&
  215.          TabIsDelete(key, table))) {
  216.         BMoveToEnd();
  217.         mptr2 = BMarkCreate();
  218.         BMoveToStart();
  219.         BRegDelete(mptr2);
  220.         BMarkDelete(mptr2);
  221.         }
  222.     if (isforward)
  223.         BMoveToEnd();
  224.     else    BMoveToStart();
  225.     BBufGoto(savebuf);
  226.     BRegCopy(mptr, kill_buf);
  227.     }
  228.  
  229.  
  230. /* ------------------------------------------------------------ */
  231.  
  232. /* Indent iterator for R_Do. */
  233.  
  234. void
  235. R_Dent()
  236.     {
  237.     int cnt;
  238.  
  239.     CLineA();
  240.     if (BIsEnd() || !BIsBeforeMark(mark)) return;
  241.  
  242.     GoToNotGrayF();
  243.     cnt = BGetCol();
  244.     WDelWhite();
  245.     BInsTabSpaces(cnt + uarg);
  246.     SearchNLF();
  247.     }
  248.  
  249.  
  250. /* ------------------------------------------------------------ */
  251.  
  252. /* Upper/lowercase the region */
  253.  
  254. void
  255. R_Do(proc)
  256.     void (*proc)();
  257.     {
  258.     FLAG isafter;
  259.  
  260.     if (isafter = BIsAfterMark(mark)) BMarkSwap(mark);
  261.     BMarkToPoint(cwin->point);
  262.     while (!BIsEnd() && BIsBeforeMark(mark)) {
  263.         (*proc)();
  264.         }
  265.     BPointToMark(cwin->point);
  266.     if (isafter) BMarkSwap(mark);
  267.     uarg = 0;
  268.     }
  269.  
  270.  
  271. /* ------------------------------------------------------------ */
  272.  
  273. /* Hard to Soft iterator for R_Do. */
  274.  
  275. void
  276. R_HToS()
  277.     {
  278.     if (!BSearchF(NL, NL) || !BIsBeforeMark(mark)) {
  279.         BMoveToEnd();
  280.         return;
  281.         }
  282.     if (BGetChar() == NL)
  283.         BCharDelete(1);
  284.     else    {
  285.         BMoveBy(-1);
  286.         BCharChange(SNL);
  287.         }
  288.     }
  289.  
  290.  
  291. /* ------------------------------------------------------------ */
  292.  
  293. /* Lowercase iterator for R_Do. */
  294.  
  295. void
  296. R_Lower()
  297.     {
  298.     BCharChange(xtolower(BGetChar()));
  299.     }
  300.  
  301.  
  302. /* ------------------------------------------------------------ */
  303.  
  304. /* Soft to Hard iterator for R_Do. */
  305.  
  306. void
  307. R_SToH()
  308.     {
  309.     if (!BSearchF(NL, SNL) || !BIsBeforeMark(mark)) {
  310.         BMoveToEnd();
  311.         return;
  312.         }
  313.     BMoveBy(-1);
  314.     if (BGetChar() == NL) {
  315.         BMoveBy(1);
  316.         BInsChar(NL);
  317.         }
  318.     else    BCharChange(NL);
  319.     }
  320.  
  321.  
  322. /* ------------------------------------------------------------ */
  323.  
  324. /* Tabify iterator for R_Do. */
  325.  
  326. void
  327. R_Tabify()
  328.     {
  329.     struct mark *mptr;
  330.     int cnt;
  331.  
  332.     if (!BSearchF(SP, SP) || !BIsBeforeMark(mark)) {
  333.         BMoveToEnd();
  334.         return;
  335.         }
  336.     if (!IsWhite()) return;
  337.  
  338.     BMoveBy(-1);
  339.     mptr = BMarkCreate();
  340.     cnt = BGetCol();
  341.     MovePastF(IsWhite);
  342.     cnt = BGetCol() - cnt;
  343.     BRegDelete(mptr);
  344.     BMarkDelete(mptr);
  345.     while (TGetWidth(TAB, BGetCol()) <= cnt) {
  346.         cnt -= TGetWidth(TAB, BGetCol());
  347.         BInsChar(TAB);
  348.         }
  349.     BInsSpaces(cnt);
  350.     }
  351.  
  352.  
  353. /* ------------------------------------------------------------ */
  354.  
  355. /* Trim whitespace from region iterator for R_Do. */
  356.  
  357. void
  358. R_Trim()
  359.     {
  360.     CLineE();
  361.     WDelWhite();
  362.     BMoveBy(1);
  363.     }
  364.  
  365.  
  366. /* ------------------------------------------------------------ */
  367.  
  368. /* Untabify iterator for R_Do. */
  369.  
  370. void
  371. R_Untabify()
  372.     {
  373.     if (!BSearchF(TAB, TAB) || !BIsBeforeMark(mark)) {
  374.         BMoveToEnd();
  375.         return;
  376.         }
  377.  
  378.     BMoveBy(-1);
  379.     BInsSpaces(TGetWidth(TAB, BGetCol()));
  380.     BCharDelete(1);        
  381.     }
  382.  
  383.  
  384. /* ------------------------------------------------------------ */
  385.  
  386. /* Uppercase iterator for R_Do. */
  387.  
  388. void
  389. R_Upper()
  390.     {
  391.     BCharChange(xtoupper(BGetChar()));
  392.     }
  393.  
  394.  
  395. /* end of REGION.C -- Region-Oriented Commands */
  396.